package com.czxy.service.impl;

import com.czxy.mapper.*;
import com.czxy.pojo.*;
import com.czxy.service.AnswerService;
import com.czxy.service.CustomsPassDetailService;
import com.czxy.vo.AnswerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author likk
 * @create 2018/9/5
 */
@Service
public class AnswerServiceImpl implements AnswerService {

    @Autowired
    private AnswerMapper answerMapper;
    @Autowired
    private RecordMapper recordMapper;
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private CustomsPassDetailMapper customsPassDetailMapper;

    @Override
    public void insert(TAnswer answer) {
        answerMapper.insert(answer);
    }

    @Override
    public TAnswer findAnswer(Integer questionId, Integer studentId) {
        TAnswer answer = new TAnswer();
        answer.setStuentId(studentId);
        answer.setQuestionId(questionId);

        return answerMapper.selectOne(answer);
    }

    @Override
    public void readOver(AnswerVo answerVo, Integer userId) {
        Integer result = answerVo.getResult();

        // 如果回答正确
        if (result == 2) {
            // 更新闯关记录

            TRecord record = new TRecord();
            record.setStudentId(userId);
            record.setCustomsPassId(answerVo.getCustomsPassId());

            TRecord tRecord = recordMapper.selectOne(record);
            // 耗时
            Date now = new Date();
            tRecord.setSpendTime((now.getTime() - tRecord.getBeginTime().getTime())/1000);

            // 计算进度
            // 关卡详情
            TCustomsPassDetail customsPassDetail = customsPassDetailMapper.selectByPrimaryKey(answerVo.getCustomsPassId());
            // 题目信息
            TQuestion question = questionMapper.selectByPrimaryKey(answerVo.getQuestionId());

            // 如果关卡题目数 = 当前题目的题号，证明当前关通关
            if (customsPassDetail.getQuestionNumber().equals(question.getQuestionNumber())) {
                // 通关
                tRecord.setProgress(100.0); //进度100%
                tRecord.setStatus(3); //设置状态为已通关

                // 开启下一关
                TRecord newRecord = new TRecord();
                newRecord.setStatus(2); //闯关中
                newRecord.setBeginTime(new Date());//开始时间
                newRecord.setSpendTime(0L);
                newRecord.setCustomsPassId(answerVo.getCustomsPassId());
                newRecord.setStudentId(userId);
                newRecord.setProgress(0.0); //进度
                newRecord.setQuestionId(customsPassDetail.getFirstQuestionId());

                recordMapper.insert(newRecord);

                // 判断是否当前学生是否为通关用时最短
                Long spendTime = tRecord.getSpendTime();
                Long leastTime = customsPassDetail.getLeastTime();
                // 如果最短通关时间还未出现，或者小于最短通关时间
                if (leastTime == null || spendTime < leastTime) {
                    customsPassDetail.setLeastTime(spendTime);

                    customsPassDetailMapper.updateByPrimaryKey(customsPassDetail);
                }
            } else {
                // 未通关
                // 开始下一题
                tRecord.setQuestionId(question.getNextQuestionId());
                // 计算进度
                tRecord.setProgress(((double) question.getQuestionNumber() + 1) / (double)customsPassDetail.getQuestionNumber());
            }

            // 通关后，清除提示信息和错误信息
            answerVo.setTips(null);
            answerVo.setCauseby(null);

        }

        // 更新答案
        answerMapper.updateByPrimaryKey(answerVo);

    }
}
